package main;

import connectDB.ConnectDB;
import DAO.DialogCalendar;
import java.awt.Color;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class frm_STAFF_A02 extends javax.swing.JFrame {

    private ConnectDB conn2 = new ConnectDB();
    private String EmpCode;
    private Date date;
    private DialogCalendar dialogCal;

//    public frm_STAFF_A02() {
//        initComponents();
//        setResizable(false);
//    }
    public frm_STAFF_A02(ConnectDB conn2, String EmpCode, Date date) {
        initComponents();
        setResizable(false);
        setLocationRelativeTo(null);
        this.conn2 = conn2;
        this.EmpCode = EmpCode;
        conn2.openConnection();
        dialogCal = new DialogCalendar();

        this.date = date;
        // Set date present cho label Date
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        lbDate.setText(dateFormat.format(date));

        // Load tên nhân viên và tên công ty 
        loadData();
    }

    public frm_STAFF_A02(String EmpCode, ConnectDB conn2) {
        initComponents();
        setResizable(false);
        setLocation(400, 200);
        setLocationRelativeTo(null);
        this.EmpCode = EmpCode;
        this.conn2 = conn2;
        conn2.openConnection();
        dialogCal = new DialogCalendar();
        // Set Date Present
        date = new Date(System.currentTimeMillis());
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        // Set date present cho label Date
        lbDate.setText(dateFormat.format(date));

        // Load tên nhân viên và tên công ty 
        loadData();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lbComName = new javax.swing.JLabel();
        lbEmpName = new javax.swing.JLabel();
        btChoose = new javax.swing.JButton();
        lbDate = new javax.swing.JLabel();
        lbTitle = new javax.swing.JLabel();
        btBack = new javax.swing.JButton();
        btReg = new javax.swing.JButton();
        btSearch = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Company Name");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Employee Name");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Date");

        lbComName.setText("XXXXXXXXXXXXX");

        lbEmpName.setText("XXXXXXXXXXXXX");

        btChoose.setText("...");
        btChoose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btChooseActionPerformed(evt);
            }
        });

        lbDate.setText("yyyy/MM/dd");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbEmpName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbComName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lbDate)
                        .addGap(18, 18, 18)
                        .addComponent(btChoose)))
                .addContainerGap(66, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lbComName, lbEmpName});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lbComName)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lbEmpName)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(btChoose)
                    .addComponent(lbDate))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lbComName, lbDate, lbEmpName});

        lbTitle.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        lbTitle.setText("INPUT DATE");

        btBack.setText("BACK");
        btBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBackActionPerformed(evt);
            }
        });

        btReg.setText("REGISTER");
        btReg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRegActionPerformed(evt);
            }
        });

        btSearch.setText("SEARCH");
        btSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btBack)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btSearch)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btReg))
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(155, 155, 155)
                        .addComponent(lbTitle)))
                .addContainerGap(65, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(lbTitle)
                .addGap(34, 34, 34)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btReg)
                    .addComponent(btSearch)
                    .addComponent(btBack))
                .addContainerGap(59, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(1, 1, 1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBackActionPerformed
        conn2.closeConnection();
        frm_STAFF_A01 frm1 = new frm_STAFF_A01();
        frm1.setVisible(true);
        setVisible(false);

    }//GEN-LAST:event_btBackActionPerformed

    private void btRegActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRegActionPerformed
        // TODO add your handling code here:
        frm_STAFF_A03 frm3 = new frm_STAFF_A03(EmpCode, conn2, date);
        frm3.setVisible(true);
        setVisible(false);
        
    }//GEN-LAST:event_btRegActionPerformed

    private void btSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed
        // TODO add your handling code here:
        frm_STAFF_A04 frm4 = new frm_STAFF_A04(conn2, EmpCode, date);
        frm4.setVisible(true);
        setVisible(false);
        
    }//GEN-LAST:event_btSearchActionPerformed

    private void btChooseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btChooseActionPerformed
        // TODO add your handling code here:

        dialogCal.MakeCalendar();
        String strDate = dialogCal.setSelectedDate();
        if (strDate.equals("")) {
            return;
        }
        lbDate.setText(strDate);
        date = dialogCal.getDate();

        //checkMode(lbDate.getText());
    }//GEN-LAST:event_btChooseActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBack;
    private javax.swing.JButton btChoose;
    private javax.swing.JButton btReg;
    private javax.swing.JButton btSearch;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lbComName;
    private javax.swing.JLabel lbDate;
    private javax.swing.JLabel lbEmpName;
    private javax.swing.JLabel lbTitle;
    // End of variables declaration//GEN-END:variables

    private void loadData() {
        ArrayList<String> arr = new ArrayList<String>();
        try {
            String query = "SELECT ";
            query += "* ";
            query += "FROM ";
            query += "EMPLOYEE_MST EMP,";
            query += "COMPANY_MST COM ";
            query += "WHERE ";
            query += "EMP.EMP_CODE = '" + EmpCode + "' ";
            query += "AND ";
            query += "EMP.COM_CODE = COM.COM_CODE";
            ResultSet rs = conn2.excuteQuery(query);
            while (rs.next()) {
                arr.add(rs.getString("EMP_FIRST_NAME").trim());
                arr.add(rs.getString("EMP_LAST_NAME").trim());
                arr.add(rs.getString("COM_NAME").trim());
            }

            lbComName.setText(arr.get(2));
            if (lbComName.getText().trim().equals("1")) {
                lbComName.setForeground(Color.red);
            }

            lbEmpName.setText((arr.get(1) + " " + arr.get(0)).toString());
        } catch (SQLException ex) {
            System.out.println("Error: " + ex.getMessage());
        }
    }
}
